﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TathamOddie.OpenSearchValidator.Logic.Tasks.Rss
{
	public abstract class XsdValidationTask : IValidationTask
	{
		string resourceName;
		Uri sourceUri;
		string xmlContent;
		string xsdContent;

		public XsdValidationTask(string resourceName, Uri sourceUri, string xmlContent, string xsdContent)
		{
			this.resourceName = resourceName;
			this.sourceUri = sourceUri;
			this.xmlContent = xmlContent;
			this.xsdContent = xsdContent;
		}

		public ValidationTaskResult Validate(ValidationServices services, ValidationState state)
		{
			var result = new ValidationTaskResult();

			var xsdResults = XsdValidator.Validate(xmlContent, xsdContent);

			if (xsdResults.Length > 0)
			{
				result.AddMessage(MessageType.Warning, "Failed while validating {0} from [{1}] against XSD. Errors follow.", resourceName, sourceUri);
				foreach (var xsdResult in xsdResults) result.Messages.Add(xsdResult);
			}
			else
			{
				result.AddMessage(MessageType.Success, "Validated {0} from [{1}] against XSD.", resourceName, sourceUri);
				OnValidateSuccess(result);
			}

			return result;
		}

		protected virtual void OnValidateSuccess(ValidationTaskResult result)
		{
		}
	}
}